<?php

namespace App\Jobs;

use App\Models\CouponLog;
use App\Models\Sys;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use function app\Utils\go;
use App\Models\CouponRule as CouponRuleModel;

class CouponJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user=$user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $coupon_on=Sys::getValue("coupon_on");
        $coupon_price=Sys::getValue("coupon_price");
        $coupon_type=Sys::getValue("coupon_type");
        if ($coupon_on==0){
            return null;
        }

        if ($coupon_type==1){
            $res=CouponRuleModel::where("day",1)->first();
            if ($res){
                $coupon_price=$res["price"];
            }
        }

        $create=[
            "uid"=>$this->user["id"],
            "invite_uid"=>$this->user["invite_uid"],
            "date"=>date("Y-m-d"),
            "day"=>1,
            "price"=>$coupon_price
        ];
        CouponLog::create($create);


    }
}
